package com.example.settlement.controller;

import com.example.settlement.entity.JoinInfo;
import com.example.settlement.entity.Settlement;
import com.example.settlement.entity.UserInfo;
import com.example.settlement.service.JoinInfoService;
import com.example.settlement.service.SettlementService;
import com.example.settlement.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(tags = "结算")
@RestController
@RequestMapping("/settlement")
public class SettlementController {
    @Autowired
    private SettlementService settlementService;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private JoinInfoService joinInfoService;

    @ApiOperation("添加结算")
    @PostMapping("/add")
    public String addSettlement(@RequestBody Settlement settlement) {
        settlementService.addSettlement(settlement);
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(settlement.getUserId());
        userInfo.setUsername("test-"+settlement.getUserId());
        userInfo.setCreateDate(settlement.getCreateTime());
        userInfoService.addSettlement(userInfo);
        return "Settlement added successfully!";
    }

    @ApiOperation("根据用户ID获取结算")
    @GetMapping("/user/{userId}")
    public List<Settlement> getSettlementsByUserId(@PathVariable Long userId) {
        return settlementService.getSettlementsByUserId(userId);
    }

    @ApiOperation("根据用户ID获取联合信息")
    @GetMapping("/join/{userId}")
    public List<JoinInfo> getJoinInfoByUserId(@PathVariable Long userId) {
        return joinInfoService.getJoinInfoByUserId(userId);
    }
}